package com.block.channelext.service.channel;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.channelext.model.result.ReportResult;
import com.block.channelext.service.IChannelHandleService;
import com.block.common.exception.BotApiException;
import com.block.common.service.LogFactory;
import com.block.db.entity.ChannelInfo;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class JiZhunChannelHandleService implements IChannelHandleService {

    private String requestUrl = "https://api-extra.hzjizhun.com/hdt-trk/hdt/adv-cvt";
//    private String authStr = "NjM5MWFhNjJiN2Y4YjBlNDEyMWY3Yzg0OmR5eENEQnZTZ2F3TU8zWjVTY3dVUUVvUnl0R0tjRC13";
    private String authStr = "NjIzMTcxMzViY2RmYjBlNDE2NDc3YmM1OmR5UnJERWpVTkNUTXkzVHBTY3dVUUVveGZQcktjMzdO";


    private static Logger log = LogFactory.getCommLog();

    @Override
    public ReportResult registerAction(ChannelInfo channelInfo, JSONObject extJson) {

        String jcid = extJson.getString("jcId");
        if (StrUtil.isEmpty(jcid)){
            return ReportResult.newInstance();
        }

        String authContent = StrUtil.format("Basic {}", authStr);

        Map<String, Object> bizMap = new HashMap<>();
        bizMap.put("jcid", jcid);
        bizMap.put("event", "event1");

        log.info("【极准上报】【注册】【开始请求】【reqestUrl】{}【bizMap】{}", requestUrl, JSON.toJSONString(bizMap));
        String response = HttpUtil.createPost(requestUrl)
                .header("Authorization", authContent)
                .body(JSON.toJSONString(bizMap))
                .execute().body();

        log.info("【极准上报】【注册】【开始结束】【result】{}", response);

        JSONObject result = JSON.parseObject(response);
        Integer code = result.getInteger("code");

        ReportResult reportResult = ReportResult.newInstance(JSON.toJSONString(bizMap), response);
        reportResult.setCode(String.valueOf(code));
        if (ObjectUtil.equal(code, Integer.valueOf(200))){
            // 上报不成功报异常
            reportResult.setSuccess(true);
        }
        return reportResult;
    }

    @Override
    public ReportResult authAction(ChannelInfo channelInfo, JSONObject extJson) {

        String jcid = extJson.getString("jcid");
        if (StrUtil.isEmpty(jcid)){
            return ReportResult.newInstance();
        }

        String authContent = StrUtil.format("Basic {}", authStr);

        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("Authorization", authContent);

        Map<String, Object> bizMap = new HashMap<>();
        bizMap.put("jcid", jcid);
        bizMap.put("event", "event2");

        log.info("【极准上报】【认证】【开始请求】【reqestUrl】{}【bizMap】{}", requestUrl, JSON.toJSONString(bizMap));
        String response = HttpUtil.createPost(requestUrl)
                .headerMap(headerMap, true)
                .body(JSON.toJSONString(bizMap))
                .execute().body();

        log.info("【极准上报】【认证】【开始结束】【result】{}", response);

        JSONObject result = JSON.parseObject(response);
        Integer code = result.getInteger("code");

        ReportResult reportResult = ReportResult.newInstance(JSON.toJSONString(bizMap), response);
        reportResult.setCode(String.valueOf(code));
        if (!ObjectUtil.equal(code, Integer.valueOf(200))){
            // 上报不成功报异常
            reportResult.setSuccess(true);
        }
        return reportResult;
    }

    @Override
    public ReportResult proUvAction(ChannelInfo channelInfo, JSONObject extJson) {

        String jcid = extJson.getString("jcid");
        if (StrUtil.isEmpty(jcid)){
            return ReportResult.newInstance();
        }

        String authContent = StrUtil.format("Basic {}", authStr);

        Map<String, Object> bizMap = new HashMap<>();
        bizMap.put("jcid", jcid);
        bizMap.put("event", "event3");

        log.info("【极准上报】【下游uv】【开始请求】【reqestUrl】{}【bizMap】{}", requestUrl, JSON.toJSONString(bizMap));
        String response = HttpUtil.createPost(requestUrl)
                .header("Authorization", authContent)
                .body(JSON.toJSONString(bizMap))
                .execute().body();
        log.info("【极准上报】【下游uv】【开始结束】【result】{}", response);

        JSONObject result = JSON.parseObject(response);
        Integer code = result.getInteger("code");

        ReportResult reportResult = ReportResult.newInstance(JSON.toJSONString(bizMap), response);
        reportResult.setCode(String.valueOf(code));
        if (!ObjectUtil.equal(code, Integer.valueOf(200))){
            // 上报不成功报异常
            reportResult.setSuccess(true);
        }
        return reportResult;
    }
}
